package com.mali.malibackendproduct.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.core.QueueBuilder;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {

    // 定义死信交换机  
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("dlx.exchange");
    }

    // 定义死信队列  
    @Bean
    public Queue dlqQueue() {
        return QueueBuilder.durable("dlq.queue")
                .withArgument("x-dead-letter-exchange", "") // 这里留空，因为不是从另一个DLQ转发  
                .build();
    }

    // 绑定死信队列到死信交换机  
    @Bean
    public Binding dlqBinding(Queue dlqQueue, DirectExchange dlxExchange) {
        return BindingBuilder.bind(dlqQueue).to(dlxExchange).with("dlq.key");
    }

    // 定义原始队列，设置死信交换机和可选的默认TTL  
    @Bean
    public Queue originalQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "dlx.exchange"); // 设置死信交换机
        return new Queue("mali.queue", true, false, false, args);
    }



    // 原始交换机
    @Bean
    public DirectExchange originalExchange() {
        return new DirectExchange("original.exchange");
    }

    // 定义原始队列与发送消息的交换机之间的绑定（需要指定路由键）
    @Bean
    public Binding originalBinding(Queue originalQueue, DirectExchange originalExchange) {
        String routingKey = "downLine"; // 你可以根据需要设置路由键
        return BindingBuilder.bind(originalQueue).to(originalExchange).with(routingKey);
    }

}